package com.flag.liutao.modules.role.controller;


import com.flag.liutao.common.domain.IdParam;
import com.flag.liutao.common.domain.ResponseResult;
import com.flag.liutao.modules.role.param.RoleMenuIdParam;
import com.flag.liutao.modules.role.param.RoleMenuParam;
import com.flag.liutao.modules.role.param.RoleQueryParam;
import com.flag.liutao.modules.role.service.IRoleMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author godM
 * @since 2022-12-02
 */
@RestController
@RequestMapping("/admin/roleMenu")
@Api(tags = "角色权限管理")
@RequiredArgsConstructor
public class RoleMenuController {

    private final IRoleMenuService roleMenuService;

    /**
     * 通过id 获取角色权限列表
     * @param param
     * @return
     */
    @GetMapping("list")
    @PreAuthorize("hasAuthority('sys_power:role_menu_R')")
    @ApiOperation(value = "获取角色权限关系",notes ="角色权限管理：获取角色权限关系" )
    public ResponseResult<Map<String,Object>> getRoleMenuList(RoleQueryParam param){
        Map<String,Object> map = roleMenuService.getRoleMenuPage(param.getId(),param.getPage(),param.getLimit(),param.getKeyword());
        return ResponseResult.ok(map);
    }

    /**
     * 删除角色权限关系
     * @param param
     * @return
     */
    @PostMapping("del")
    @ApiOperation(value="删除角色权限关系",notes ="角色权限管理：删除角色权限关系")
    public ResponseResult<Boolean> delRoleMenu(@Validated @RequestBody RoleMenuIdParam param){
        roleMenuService.delRoleMenu(param.getRoleId(),param.getMenuId());
        return ResponseResult.ok();
    }



    /**
     * 通过id 获取角色未授权权限列表
     * @param param
     * @return
     */
    @GetMapping("noList")
    @PreAuthorize("hasAuthority('sys_power:role_menu_R')")
    @ApiOperation(value = "获取角色没有的权限",notes ="角色权限管理：获取角色没有的权限关系" )
    public ResponseResult<Map<String,Object>> getRoleNoHasList(RoleQueryParam param){
        Map<String,Object> map = roleMenuService.getRoleNoHasList(param.getId());
        return ResponseResult.ok(map);
    }

    @PostMapping("add")
    @PreAuthorize("hasAuthority('sys_power:role_menu_C')")
    @ApiOperation(value = "添加角色权限关系",notes ="角色权限管理：添加角色权限关系" )
    public ResponseResult<Boolean> addRoleMenu(@Validated @RequestBody RoleMenuParam param){
        roleMenuService.addRoleMenu(param.getRoleId(),param.getMenuIdList());
        return ResponseResult.ok();
    }
}
